#include "StdAfx.h"
#include "DataEventHandler.h"


void rdc_transport::DataEventHandler::RegisterWriter(dataEventDelegate writeComplite, dataEventDelegate writeCancelled)
{
    boost::ptr_map<UINT, signal_t>::iterator itComplite = signals.find(CHANNEL_EVENT_WRITE_COMPLETE);
    if(itComplite == signals.end())
        throw std::runtime_error(std::string(__FUNCTION__) + "Cannot find event");

    itComplite->second->connect(writeComplite);

    boost::ptr_map<UINT, signal_t>::iterator itCancelled = signals.find(CHANNEL_EVENT_WRITE_CANCELLED);
    if(itCancelled == signals.end())
        throw std::runtime_error(std::string(__FUNCTION__) + "Cannot find event");

    itCancelled->second->connect(writeCancelled);
}

void rdc_transport::DataEventHandler::RegisterReader(dataEventDelegate dataReceived)
{
    boost::ptr_map<UINT, signal_t>::iterator it = signals.find(CHANNEL_EVENT_DATA_RECEIVED);
    if(it == signals.end())
        throw std::runtime_error(std::string(__FUNCTION__) + "Cannot find event");

    it->second->connect(dataReceived);
}

rdc_transport::DataEventHandler::DataEventHandler()
{
    signals.insert(CHANNEL_EVENT_DATA_RECEIVED, std::auto_ptr<signal_t>(new signal_t));
    signals.insert(CHANNEL_EVENT_WRITE_COMPLETE, std::auto_ptr<signal_t>(new signal_t));
    signals.insert(CHANNEL_EVENT_WRITE_CANCELLED, std::auto_ptr<signal_t>(new signal_t));
}

boost::signal<void(LPVOID, UINT32, UINT32, UINT32)>& 
rdc_transport::DataEventHandler::OnEvent(UINT event)
{
    boost::ptr_map<UINT, signal_t>::iterator it = signals.find(event);
    assert(it!= signals.end());

    return *it->second;
}
